//
//  TTLiveBriefView.swift
//  TTProduct
//
//  Created by Toj on 3/24/21.
//  Copyright © 2021 TTProduct. All rights reserved.
//

import Foundation

class TTLiveBriefView: TTLiveStyleView {
    
    override var model: NSObject? {
        didSet {
            let stringNumber = arc4random_uniform(100000)
            residentView.nicknameLabel.stringValue = "博伟\(stringNumber)"
        }
    }
    
    // 悬浮: 展示View
    // 举手
    lazy var handButton: TTButton = {
        let view = TTButton()
        view.tt_setImage(NSImage(named: "liveItemhands"), for: .normal)
        return view
    }()
    
    // 常驻: 展示信息
    lazy var residentView: TTLiveBriefResidentView = {
        let view = TTLiveBriefResidentView()
        view.zoomButton.tt_addTarget(self, action: #selector(self.didTapZoom(sender:)), for: .leftMouseUp)
        return view
    }()
   
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        
        setupMakeAddSubviews()
        setupMakeUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layout() {
        super.layout()
        
        setupMakeLayoutSubviews()
    }
}

extension TTLiveBriefView {
    private func setupZoom(is selected: Bool) {
        var normal  = "liveBriefEnlargedNormal"
        var hover   = "liveBriefEnlargedHover"
        var pressed = "liveBriefEnlargedPressed"
        
        if selected {
            normal  = "liveBriefResumeNormal"
            hover   = "liveBriefResumeHover"
            pressed = "liveBriefResumePressed"
        }
        
        residentView.zoomButton.tt_setImage(NSImage(named: normal), for: .normal)
        residentView.zoomButton.tt_setImage(NSImage(named: hover), for: .hover)
        residentView.zoomButton.tt_setImage(NSImage(named: pressed), for: .press)
    }
}

extension TTLiveBriefView {
    
    @objc private func didTapZoom(sender: TTButton) {
        sender.tt_selected = !sender.tt_selected
        
        setupZoom(is: sender.tt_selected)
    }
}

extension TTLiveBriefView {
    
    private func setupMakeAddSubviews() {
        
        addSubview(residentView)
        addSubview(handButton)
    }
    
    private func setupMakeLayoutSubviews() {
        
        residentView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(self)
            make.height.equalTo(23)
        }
        
        handButton.snp.makeConstraints { (make) in
            make.width.equalTo(17)
            make.height.equalTo(17)
            make.right.equalTo(snp.right).offset(-4)
            make.top.equalTo(4)
        }
    }
    
    private func setupMakeUI() {
        // handButton.isHidden = true
        setupZoom(is: false)
    }
}
